/* eslint-disable @typescript-eslint/no-var-requires */
const COS = require('cos-nodejs-sdk-v5');
const fs = require('fs');
import { secretId, secretKey } from '../../const/index'; 
import { UUID36 } from '../index';

// 使用永久密钥进行上传
const cos = new COS({
  SecretId: secretId,
  SecretKey: secretKey,
});

// 上传图片
export const uploadFileService = (options: IAnyObject) => {
  return new Promise((resolve, reject) => {
    cos.putObject({
      Bucket: 'csuxzy-1300770696', /* 必须 */
      Region: 'ap-guangzhou',    /* 必须 */
      Key: `${UUID36(10)}${options.name}`, /* 必须 */
      StorageClass: 'STANDARD',
      Body: fs.createReadStream(options.path), // 上传文件对象
      onProgress: function(progressData: IAnyObject) {
        console.log(JSON.stringify(progressData), '?????')
        // 这里做上传进度的操作，暂时先不写了QAQ
      }
    }, function(err: IAnyObject, data: IAnyObject) {
      if (data) {
        resolve(data);
      }
      if (err) {
        reject(err);
      }
    })
  })
};

// 上传base64
export const uploadFileBase64Service = (options: IAnyObject) => {
  return new Promise((resolve, reject) => {
    cos.putObject({
      Bucket: 'csuxzy-1300770696', /* 必须 */
      Region: 'ap-guangzhou',    /* 必须 */
      Key: `${UUID36(10)}${options.name}`, /* 必须 */
      StorageClass: 'STANDARD',
      Body: options.data, // 上传文件对象
      onProgress: function(progressData: IAnyObject) {
        console.log(JSON.stringify(progressData), '?????')
        // 这里做上传进度的操作，暂时先不写了QAQ
      }
    }, function(err: IAnyObject, data: IAnyObject) {
      if (data) {
        resolve(data);
      }
      if (err) {
        reject(err);
      }
    })
  })
};

// 删除图片
export const deleteFile = () => {

};